package com.kasax.skin.support;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.TextView;

import com.kasax.skin.support.utils.LogUtils;

public class SkinResource {
    private static SkinResource instance;
    private Resources mAppResource;
    // 是否使用默认皮肤
    private boolean isDefaultSkin = true;
    private Resources mSkinResource;
    private String mSkinPkgName;

    private SkinResource() {

    }

    private SkinResource(Context context) {
        mAppResource = context.getResources();
    }

    public static void init(Context context) {
        if (instance == null) {
            synchronized (SkinResource.class) {
                if (instance == null) {
                    instance = new SkinResource(context);
                }
            }
        }

    }

    public static SkinResource getInstance() {
        return instance;
    }

    /**
     * 启用换肤
     *
     * @param resources
     * @param pkgName
     */
    public void applySkin(Resources resources, String pkgName) {
        mSkinResource = resources;
        mSkinPkgName = pkgName;
        // 是否使用默认皮肤
        isDefaultSkin = TextUtils.isEmpty(pkgName) || resources == null;
    }

    /**
     * 获取字体对象
     *
     * @param skinTypeFaceId
     * @return
     */
    public Typeface getTypeface(int skinTypeFaceId) {
        String skinTypefacePath = getString(skinTypeFaceId);
        if (TextUtils.isEmpty(skinTypefacePath)) {
            return Typeface.DEFAULT;
        }
        try {
            if (isDefaultSkin) {
                return Typeface.createFromAsset(mAppResource.getAssets(), skinTypefacePath);
            }
            return Typeface.createFromAsset(mSkinResource.getAssets(), skinTypefacePath);
        } catch (Exception e) {
            LogUtils.e(getClass(), e);
        }
        return Typeface.DEFAULT;
    }

    private String getString(int skinTypeFaceId) {
        try {
            //使用默认皮肤
            if (isDefaultSkin) {
                //使用app 设置的属性值
                return mAppResource.getString(skinTypeFaceId);
            }

            int skinId = getIdentifier(skinTypeFaceId);
            if (skinId == 0) {
                //使用app 设置的属性值
                return mAppResource.getString(skinTypeFaceId);
            }
            return mSkinResource.getString(skinId);
        } catch (Resources.NotFoundException e) {
            LogUtils.e(getClass(), e);
        }
        return null;
    }

    private int getIdentifier(int resId) {
        if (isDefaultSkin) {
            return resId;
        }
        //在皮肤包中不一定就是 当前程序的 id
        //获取对应id 在当前的名称 colorPrimary
        //R.drawable.ic_launcher
        String resName = mAppResource.getResourceEntryName(resId); //ic_launcher   /colorPrimaryDark
        String resTypeName = mAppResource.getResourceTypeName(resId);//drawable
        int skinId = mSkinResource.getIdentifier(resName, resTypeName, mSkinPkgName);
        return skinId;
    }

    /**
     * 可能是Color 也可能是drawable
     *
     * @param resId
     * @return
     */
    public Object getBackground(int resId) {
        String resourceTypeName = mAppResource.getResourceTypeName(resId);
        if (resourceTypeName.equals("color")) {
            return getColor(resId);
        } else {
            return getDrawable(resId);
        }
    }

    public int getColor(int resId) {
        if (isDefaultSkin) {
            return mAppResource.getColor(resId);
        }

        int skinId = getIdentifier(resId);
        if (skinId == 0) {
            return mAppResource.getColor(resId);
        }
        return mSkinResource.getColor(skinId);
    }

    public Drawable getDrawable(int resId) {
        //如果有皮肤  isDefaultSkin false 没有就是true
        if (isDefaultSkin) {
            return mAppResource.getDrawable(resId);
        }

        int skinId = getIdentifier(resId);
        if (skinId == 0) {
            return mAppResource.getDrawable(resId);
        }
        return mSkinResource.getDrawable(skinId);
    }

    public ColorStateList getColorStateList(int resId) {
        if (isDefaultSkin) {
            return mAppResource.getColorStateList(resId);
        }
        int skinId = getIdentifier(resId);
        if (skinId == 0) {
            return mAppResource.getColorStateList(resId);
        }
        return mSkinResource.getColorStateList(skinId);
    }
}
